﻿@page
@model LoginModel
@inject Eqwww.Config.IAppConfigProvider AppConfigProvider
@inject Eqwww.App.Identity.UserApp UserApp
@{
    ViewData["Title"] = "登录";

    Request.Cookies.TryGetValue("LastLoginUserId", out string LastLoginId);
    Eqwww.Domain.Models.Identity.UserModel LastLoginUser = null;
    if (!string.IsNullOrEmpty(LastLoginId))
    {
        LastLoginUser = UserApp.FindByIdAsync(LastLoginId).Result;
    }

}

<style type="text/css">
    .mdui-textfield-error {
        visibility: visible;
    }

    .mdui-textfield .mdui-icon ~ .mdui-textfield-error {
        margin-left: 0;
    }
</style>

<!--
<div style="position:fixed; left:0; top:0; width:100%;height:100%; z-index:-100; filter:brightness(70%); background-image: url(/images/06151.jpg); background-size: cover;background-position:center center;">
</div>
-->

<div class="mdui-row">
    <div class="mdui-col-sm-6 mdui-col-offset-sm-3 mdui-col-md-4 mdui-col-offset-md-4">
        <div class="mdui-p-t-5"></div>
        <div class="mdui-shadow-1 mdui-p-a-3 mdui-color-white" style="border-radius:5px;">
            <form id="account" method="post">

                @if (LastLoginUser != null)
                {
                    <div class="mdui-text-center">
                        <img src="@LastLoginUser.Avatar" class="mdui-img-circle" style="width:128px;" />
                    </div>
                    <div class="mdui-text-center">
                        <span class="mdui-typo-display-1">@LastLoginUser.RealName</span><br />
                        @LastLoginUser.UserName
                        <input type="hidden" asp-for="Input.Email" value="@LastLoginUser.UserName" />
                        @{
                            bool IsRTX = false;
                            if (LastLoginUser.UserType == UserType.RTX)
                            {
                                IsRTX = true;
                            }
                        }
                        <input type="hidden" asp-for="Input.RememberMe" value="@IsRTX" />
                    </div>

                    <div class="mdui-text-right">
                        <a onclick="btn_changeaccount()" class="mdui-btn mdui-text-color-theme" href="javascript:;">切换账号</a>
                    </div>
                }
                else
                {
                    <div class="mdui-textfield mdui-textfield-floating-label">
                        <label asp-for="Input.Email" class="mdui-textfield-label"></label>
                        <input asp-for="Input.Email" class="mdui-textfield-input mdui-color-grey-100" />
                        <i class="mdui-icon material-icons" style="position:absolute;right:0;">account_circle</i>
                        <span asp-validation-for="Input.Email" class="mdui-textfield-error"></span>
                    </div>
                }
                <div class="mdui-textfield  mdui-textfield-floating-label">
                    <label asp-for="Input.Password" class="mdui-textfield-label"></label>
                    <input asp-for="Input.Password" class="mdui-textfield-input mdui-color-grey-100" />
                    <i class="mdui-icon material-icons" style="position:absolute;right:0;">lock_outline</i>
                    <span asp-validation-for="Input.Password" class="mdui-textfield-error"></span>
                </div>

                @if (LastLoginUser == null)
                {
                    
                        var appConfig = AppConfigProvider.Load();

                    
                    @if (appConfig.UseADLogin)
                    {
                        <div class="mdui-list">
                            <div class="mdui-list-item mdui-ripple">


                                @if (appConfig.LoginRemberMeUseToggle)
                                {
                                    <label asp-for="Input.RememberMe" class="mdui-switch">
                                        <input asp-for="Input.RememberMe" />
                                        <i class="mdui-switch-icon"></i>
                                    </label>
                                }
                                else
                                {
                                    <label class="mdui-checkbox">
                                        <input asp-for="Input.RememberMe" />
                                        <i class="mdui-checkbox-icon"></i>
                                    </label>
                                }

                                <div class="mdui-list-item-content">@Html.DisplayNameFor(modelItem => modelItem.Input.RememberMe)</div>
                            </div>


                        </div>
                    }

                }

                <div class="mdui-textfield mdui-text-right">
                    <button type="button" id="btn_login" class="mdui-btn mdui-ripple mdui-color-theme">登录</button>
                </div>
                <p class="mdui-text-right">
                    <a class="mdui-btn mdui-text-color-theme" href="/Identity/Account/Register">注册</a>
                </p>
            </form>
        </div>
    </div>

</div>

@section Scripts {
    <partial name="_ValidationScriptsPartial" />

    <script>
        var $$ = mdui.JQ;

        function btn_changeaccount() {
            $.cookie("LastLoginUserId", '', { expires: -1, path: "/" });
            window.location.reload();
        }

        function btn_login() {
            if (!$("#account").valid()) {
                return false;
            }

            var $this = $('#btn_login');
            $this.attr('disabled', 'disabled');
            $.loading(false);
            var postData = $('#account').formSerialize();

            postData['Input.Email'] = $('#Input_Email').val();
            postData["Input.Password"] = $('#Input_Password').val();

            @if (LastLoginUser != null)
            {
                <text>postData['Input.RememberMe'] = $('#Input_RememberMe').val();</text>
            }
            else
            {
                <text>postData['Input.RememberMe'] = $('#Input_RememberMe').is(':checked');</text>
            }


                $.ajax({
            url: '/Identity/Account/Login',
                    type: 'post',
                    data: postData,
                    dataType: 'json',
                    async: true,
                    success: function (data) {

                        $.loading(true);
                    if (data.state == 'success') {
                        mdui.snackbar('登录成功!正在跳转...');
                        //$('#btn_login').addClass('mdui-text-color-theme').html('登录成功！正在跳转...');
                        window.location.href = '/Manage';

                    }
                    else {
                            $this.removeAttr('disabled');
                            top.$.errorLayer(data.message);
                    }
                }
            });
        }

        $(function () {
            $('#btn_login').bind('click', function () {

                btn_login();

            });


            $(document).keydown(function (event) {
                if (event.keyCode == 13) {
                    btn_login();
                }
            });
        });
    </script>
}
